package com.imb.provider.service;

import cn.hutool.core.bean.BeanUtil;
import com.imb.common.core.enums.PrErrorEnum;
import com.imb.common.mybatis.entity.ImbUser;
import com.imb.common.mybatis.mapper.ImbUserMapper;
import com.imb.common.mybatis.plus.ServicePlusImpl;
import com.imb.common.web.exception.CustomException;
import com.imb.common.web.model.LoginUser;
import com.imb.common.web.model.UserDetail;
import com.imb.provider.vo.UserInfoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

/**
 * @author xzhi
 * @desc
 * @date 2024-09-25
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServicePlusImpl<ImbUserMapper, ImbUser> implements UserService {
    
    private final ImbUserMapper imbUserMapper;
    
    @Override
    public UserInfoVo queryUserInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        boolean authenticated = authentication.isAuthenticated();
        if (!authenticated){
            throw new CustomException(PrErrorEnum.ERROR_1002.getInfo(),PrErrorEnum.ERROR_1002.getCode());
        }
        LoginUser userDetails = (LoginUser) authentication.getPrincipal();
        UserDetail user = userDetails.getUser();
        return BeanUtil.toBean(user,UserInfoVo.class);
    }
}
